/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp.view.agent;

import java.awt.GridLayout;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ca.usherbrooke.agent.controller.shared.IAgent;
import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.equipment.ProtectionModel;
import ca.usherbrooke.model.equipment.WeaponModel;
import ca.usherbrooke.tools.position.Vector2D;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EntityPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private final IAgent entity;

	private final JTextField currentScoreTextField;
	private final JTextField maxHpTextField;
	private final JTextField currentHpTextField;
	private final JTextField currentSpeedTextField;
	private final JTextField jumpReloadTimeTextField;
	private final JTextField remainJumpTimeTextField;
	private final JTextField remainAttackTimeTextField;
	private final JTextField maximumCapacityTextField;
	private final JTextField currentCapacityUsedTextField;

	private final JTextField currentAttaqueTextField;
	private final JTextField currentDefenseTextField;

	private final JTextField currentWeaponTextField;
	private final JTextField currentEquipmentTextField;

	public EntityPanel(IAgent getter) {
		super();
		entity = getter;

		this.setLayout(new GridLayout(14, 2));

		currentScoreTextField = new JTextField();
		maxHpTextField = new JTextField();
		currentHpTextField = new JTextField();
		currentSpeedTextField = new JTextField();
		jumpReloadTimeTextField = new JTextField();
		remainJumpTimeTextField = new JTextField();
		remainAttackTimeTextField = new JTextField();

		maximumCapacityTextField = new JTextField();
		currentCapacityUsedTextField = new JTextField();

		currentAttaqueTextField = new JTextField();
		currentDefenseTextField = new JTextField();
		currentWeaponTextField = new JTextField();
		currentEquipmentTextField = new JTextField();

		this.add(new JLabel("nom: "));
		this.add(new JLabel(entity.getEntity().getGuid().toString()));

		this.add(new JLabel("score actuelle: "));
		this.add(currentScoreTextField);

		this.add(new JLabel("Max HP: "));
		this.add(maxHpTextField);

		this.add(new JLabel("Current HP: "));
		this.add(currentHpTextField);

		this.add(new JLabel("Current Speed: "));
		this.add(currentSpeedTextField);

		this.add(new JLabel("Jump reload time: "));
		this.add(jumpReloadTimeTextField);

		this.add(new JLabel("Remaining reload jump: "));
		this.add(remainJumpTimeTextField);

		this.add(new JLabel("Remaining reload attack: "));
		this.add(remainAttackTimeTextField);

		this.add(new JLabel("Maximum capacity: "));
		this.add(maximumCapacityTextField);

		this.add(new JLabel("Current capacity: "));
		this.add(currentCapacityUsedTextField);

		this.add(new JLabel("Attack modifier: "));
		this.add(currentAttaqueTextField);

		this.add(new JLabel("Defense: "));
		this.add(currentDefenseTextField);

		this.add(new JLabel("Current Weapon: "));
		this.add(currentWeaponTextField);

		this.add(new JLabel("Current Defense quantity: "));
		this.add(currentEquipmentTextField);

	}

	public void reloadData() {
		IEntityModel currentEntity = entity.getEntity();

		if (currentEntity == null) {
			return;
		}

		Float currentScore = entity.getGoals().getScore();
		Integer maxHp = currentEntity.getMaxHP();
		Integer currentHp = currentEntity.getCurrentHP();
		Vector2D currentSpeed = currentEntity.getCurrentSpeed();
		Integer jumpReloadTime = currentEntity.getJumpReloadTime();
		Integer remainJumpReloadTime = currentEntity.getRemaningJumpReload();

		Integer remainAttackReloadTime = currentEntity.getRemainingAttackTime();

		Integer currentCapacityUsed = currentEntity.getCapacity();
		Integer maximumCapacity = currentEntity.getMaxCapacity();

		Integer attackModifier = currentEntity.getAttackModifier();
		Integer defense = currentEntity.getDefense();

		WeaponModel weapon = currentEntity.getCurrentWeapon();
		List<ProtectionModel> protections = currentEntity
				.getCurrentProtection();

		DecimalFormat df = new DecimalFormat("0.00");

		currentScoreTextField.setText(df.format(currentScore));
		maxHpTextField.setText(maxHp.toString());
		currentHpTextField.setText(currentHp.toString());
		currentSpeedTextField.setText("(" + df.format(currentSpeed.getX())
				+ " | " + df.format(currentSpeed.getY()) + ")");
		jumpReloadTimeTextField.setText(jumpReloadTime.toString());
		remainJumpTimeTextField.setText(remainJumpReloadTime.toString());
		remainAttackTimeTextField.setText(remainAttackReloadTime.toString());

		maximumCapacityTextField.setText(maximumCapacity.toString());
		currentCapacityUsedTextField.setText(currentCapacityUsed.toString());

		currentAttaqueTextField.setText(attackModifier.toString());
		currentDefenseTextField.setText(defense.toString());

		if (weapon != null) {
			currentWeaponTextField.setText(weapon.getName());
		} else {
			currentWeaponTextField.setText("none");
		}
		currentEquipmentTextField.setText(String.valueOf(protections.size()));

		this.revalidate();
	}
}
